package controllers

import (
	"errors"
	"github.com/revel/revel"
	"gopkg.in/mgo.v2"
	"io/ioutil"
	"github.com/astaxie/beego/logs"
)

type App struct {
	*revel.Controller
}

func (c App) Index() revel.Result {
	b, err := ioutil.ReadAll(c.Request.Body)
	if err != nil {
		revel.ERROR.Println(err)
		return nil
	}
	revel.INFO.Println(string(b))
	return c.RenderJson(errors.New("haha"))
}

	var Log *logs.BeeLogger
func init() {
	Log = logs.NewLogger(10000)
	Log.SetLogger("console", "")
	Log.EnableFuncCallDepth(true)
	Log.SetLogger("file", `{"filename":"waterblabla.log"}`)
	Log.SetLogger("smtp", `{"level":3,"username":"songkemail@163.com","password":"songke198916","host":"smtp.163.com:465","sendTos":["songkemail@163.com"]}`)
}
func (c App) Test() revel.Result {
	//Log.Critical("fuck you")
	Log.Warn("fuck you")
	Log.Info("fuck you")
	Log.Debug("fuck you")
	b, err := ioutil.ReadAll(c.Request.Body)
	if err != nil {
		revel.ERROR.Println(err)
		return nil
	}
	revel.INFO.Println(string(b))
	return nil
}

var sess *mgo.Session

func session() *mgo.Session {

	if sess == nil {
		var err error
		// TODO:
		if sess, err = mgo.Dial("root:root@localhost:27017"); err != nil {
			panic(err)
		}
	}
	return sess.Clone()
}

func DO(db, col string, f func(*mgo.Collection) error) error {
	s := session()
	defer func() {
		s.Close()
	}()

	return f(s.DB(db).C(col))
}

/*
func (this *Mongo) FindOne(selector bson.M, content interface{}) error {
	return DO(this.Db, this.Col, func(c *mgo.Collection) (err error) {
		return c.Find(selector).One(content)
	})
}

func (this *Mongo) SelectOne(query, selector bson.M, content interface{}) error {
	return DO(this.Db, this.Col, func(c *mgo.Collection) (err error) {
		return c.Find(query).One(selector)
	})
}

func (this *Mongo) FindAll(selector bson.M, out interface{}) error {
	return DO(this.Db, this.Col, func(c *mgo.Collection) (err error) {
		return c.Find(selector).All(out)
	})
}

func (this *Mongo) FindAllWithLimit(selector bson.M, count int, out interface{}) error {
	return DO(this.Db, this.Col, func(c *mgo.Collection) (err error) {
		return c.Find(selector).Limit(count).All(out)
	})
}

func (this *Mongo) FindAllWithLimitSort(selector bson.M, count int, out interface{}, sort string) error {
	return DO(this.Db, this.Col, func(c *mgo.Collection) (err error) {
		return c.Find(selector).Sort(sort).Limit(count).All(out)
	})
}

func (this *Mongo) SelectAll(query, selector bson.M, out interface{}) error {
	return DO(this.Db, this.Col, func(c *mgo.Collection) (err error) {
		return c.Find(query).Select(selector).All(out)
	})
}

func (this *Mongo) Create(content interface{}) error {
	return DO(this.Db, this.Col, func(c *mgo.Collection) (err error) {
		return c.Insert(content)
	})
}

func (this *Mongo) Delete(selector bson.M) error {
	return DO(this.Db, this.Col, func(c *mgo.Collection) (err error) {
		_, err = c.RemoveAll(selector)
		return
	})
}

func (this *Mongo) Update(selector bson.M, content interface{}) error {
	return DO(this.Db, this.Col, func(c *mgo.Collection) (err error) {
		return c.Update(selector, content)
	})
}

func (this *Mongo) Upsert(selector bson.M, content interface{}) error {
	return DO(this.Db, this.Col, func(c *mgo.Collection) (err error) {
		_, err = c.Upsert(selector, content)
		return err
	})
}

func (this *Mongo) BulkInsert(docs []interface{}) error {

		return DO(this.Db, this.Col, func(c *mgo.Collection) (err error) {
			bulk := c.Bulk()
			for _, doc := range docs {
				bulk.Insert(doc)
			}
			_, err = bulk.Run()
			return err
		})

	return nil
}*/
